<template>
	<view>
		<u--form labelPosition="left" :model="model1" :rules="rules" ref="uForm" labelWidth="100"
			:labelStyle="{color: '#fff' }">
			<view class="plr15 bg-default1">
				<u-form-item class="u-form-item" :label="$t('text.paixu')" prop="videoInfo.weigh" borderBottom ref="item1">
					<u--input v-model="model1.videoInfo.weigh" border="none" color="rgba(255, 255, 255, 0.9)"
						placeholderStyle="rgba(255, 255, 255, 0.4)" :placeholder="$t('text.placeholders')"></u--input>
				</u-form-item>
				<u-form-item class="u-form-item" :label="$t('text.zhangjiebiaoti')" prop="videoInfo.name" borderBottom ref="item1">
					<u--input v-model="model1.videoInfo.name" border="none" color="rgba(255, 255, 255, 0.9)"
						placeholderStyle="rgba(255, 255, 255, 0.4)":placeholder="$t('text.placeholders')"></u--input>
				</u-form-item>
				<u-form-item class="u-form-item" :label="$t('text.putjiage')" prop="videoInfo.price" borderBottom ref="item1">
					<u--input v-model="model1.videoInfo.price" border="none" color="rgba(255, 255, 255, 0.9)"
						type="number" placeholderStyle="rgba(255, 255, 255, 0.4)" :placeholder="$t('text.placeholders')"></u--input>
				</u-form-item>
				<u-form-item class="u-form-item" :label="$t('text.ipVip')" prop="videoInfo.vipprice" borderBottom ref="item1">
					<u--input v-model="model1.videoInfo.vipprice" border="none" color="rgba(255, 255, 255, 0.9)"
						type="number" placeholderStyle="rgba(255, 255, 255, 0.4)" :placeholder="$t('text.placeholders')"></u--input>
				</u-form-item>
				<u-form-item class="u-form-item" :label="$t('text.jieshao')" prop="videoInfo.text" borderBottom ref="item1">
					<u--input v-model="model1.videoInfo.text" border="none" color="rgba(255, 255, 255, 0.9)"
						placeholderStyle="rgba(255, 255, 255, 0.4)" :placeholder="$t('text.placeholders')"></u--input>
				</u-form-item>
				<u-form-item class="u-form-item" labelPosition="top" :label="$t('text.zhangjieshipingdizhi')" prop="videoInfo.videourl"
					ref="item1">
					<u-upload class="mt10" :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1"
						multiple :maxCount="1" accept="video" uploadIcon="play-circle-fill"></u-upload>
				</u-form-item>
				
			</view>
			<!-- <view class="plr15 bg-default1 mt10">
				<view v-for="(item,index) in jishuList" :key="index">
					<u-form-item class="u-form-item" label="影视集数" prop="videoInfo.star" ref="item1">
						<u--input v-model="jishu[index].num" border="none" color="rgba(255, 255, 255, 0.9)"
							placeholderStyle="rgba(255, 255, 255, 0.4)" placeholder="请输入" type="number"></u--input>
						<u-icon slot="right" name="close-circle" color="#fff" @click="closeJishu(index)"></u-icon>
					</u-form-item>
					<u-upload :fileList="jishu[index].file" @afterRead="afterRead" @delete="deletePic" name="2" multiple
						:maxCount="1" accept="video"></u-upload>
				</view>
			</view> -->
		</u--form>
		<!-- <view class="add" @click="tjge()">
			<view class="btn">+ 增加</view>
		</view> -->
		<!-- <u-action-sheet :show="showSex" :actions="actions" title="请选择性别" description="如果选择保密会报错"
			@close="showSex = false" @select="sexSelect">
		</u-action-sheet> -->
		<!-- 选择是否收费 -->

		<view style="height: 150rpx;"></view>
		<view class="release">
			<view class="btn" @click="$u.throttle(submit, 1000)">{{$t('text.submit')}}</view>
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				model1: {
					videoInfo: {
						weigh: '',
						pid: '',
						name: '',
						videourl: '',
						uid: uni.getStorageSync("userinfo").id,
						price: '',
						vipprice: '',
						text: '',
					},
				},
				rules: {
					'videoInfo.weigh': {
						type: 'number',
						required: true,
						message: this.$t('text.pls1'),
						trigger: ['blur', 'change']
					},
					'videoInfo.name': {
						type: 'string',
						required: true,
						message: this.$t('text.pls2'),
						trigger: ['blur', 'change']
					},
					'videoInfo.price': {
						type: 'string',
						required: true,
						message: this.$t('text.pls3'),
						trigger: ['blur', 'change']
					},
					'videoInfo.vipprice': {
						type: 'string',
						required: true,
						message: this.$t('text.ipVip'),
						trigger: ['blur', 'change']
					}
				},
				fileList1: [],

				jishuList: [],
				jishu: []
			}
		},
		onLoad(e) {
			this.tjge()
			//console.log(111)
			if (e.id > 0) {
				// this.model1.videoInfo.pid = e.id
				this.getinfo()
				// console.log(e.id)
			}
			if (e.pid > 0) {
				this.model1.videoInfo.pid = e.pid
			}

		},

		methods: {

			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: "uploading",
						message: this.$t('payee.upload'),
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "",
							url: result,
						})
					);
					fileListLen++;
				}
			},

			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.configs.webUrl + '/api/Common/upload?token=' + uni.getStorageSync(
							"userinfo").token, // 仅为示例，非真实的接口地址
						filePath: url,
						name: "file",
						formData: {
							'token': uni.getStorageSync("userinfo").token,
						},
						success: (res) => {
							var dataarr = JSON.parse(res.data);
							if (dataarr.code == 1) {
								console.log(dataarr.data.url)
								this.model1.videoInfo.videourl = this.configs.imgUrl + dataarr.data.url
								resolve(this.configs.imgUrl + dataarr.data.url)
								console.log(this.fileList1)

							} else {
								uni.showToast({
									title: dataarr.msg
								})
							}
							// console.log(dataarr)
							// setTimeout(() => {
							// 	resolve(res.data.data);
							// }, 1000);
						},
					});
				});
			},
			//添加集数
			tjge() {
				this.jishuList.push('1');
				this.jishu.push({
					num: '',
					file: [],
				})
			},

			//删除集数
			closeJishu(index) {
				let that = this
				uni.showModal({
					title: this.$t('text.shanc'),
					success(res) {
						if (res.confirm) {
							that.jishuList.splice(index, 1)
							that.jishu.splice(index, 1)
							if (that.jishu.length == 0 || that.jishuList.length == 0) {
								that.jishu = []
								that.jishuList = []
							}
						}
					}
				})
			},
			//提交表单
			submit() {
				this.$refs.uForm.validate().then(async ress => {
					console.log(this.model1.videoInfo)
					// uni.$u.toast('校验通过')
					let params = this.model1.videoInfo
					let res = await this.$httpas.post('/api/creator/zhangjie_add', params, {
						token: true
					});
					if (res[1].data.code == 1) {
						this.$u.toast(res[1].data.msg)
						setTimeout(() => {
							uni.navigateBack()
						}, 1000)
					}
				})

			},
			//获取视频详情
			getinfo() {
				this.$httpas.post2({
					data: {
						do: 'creator',
						op: 'zhangjie_info',
						id: this.model1.videoInfo.pid
					},
					success: res => {
						this.model1.videoInfo.weigh = res.weigh
						this.model1.videoInfo.name = res.name
						this.model1.videoInfo.videourl = res.videourl
						this.model1.videoInfo.uid = res.uid
						this.model1.videoInfo.price = res.price
						this.model1.videoInfo.vipprice = res.vipprice
						this.model1.videoInfo.text = res.info
						this.model1.videoInfo.pid = res.pid
						this.model1.videoInfo.id = res.id
						 this.fileList1.push({
							name:res.videourl,
							url:res.videourl,
							thumb:res.videourl,
						});
						// this.list = res 
						// this.model1.videoInfo.isPrivate = res.story

					}
				})

			},
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.uForm.setRules(this.rules)
		},
	}
</script>

<style scoped lang="scss">
	.release {
		position: fixed;
		bottom: 0;
		right: 0;
		left: 0;
		background: #000;
		padding: 30rpx;

		.btn {
			background: rgba(11, 144, 249, 1);
			text-align: center;
			border-radius: 50rpx;
			padding: 20rpx 0;
			color: #fff;
		}
	}

	.add {
		width: 100%;
		padding: 30rpx;

		.btn {
			border: 1px solid #C3C4C4;
			text-align: center;
			border-radius: 50rpx;
			padding: 16rpx 0;
			color: #fff;
		}
	}

	.u-form-item {
		::v-deep.u-form-item__body {
			padding: 30rpx 0;
		}
	}

	.u--input {
		color: #fff;
	}
</style>